package com.itheima.controller;

import com.itheima.Utils.DateUtils;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@Controller
public class MemberController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private MemberService memberService;

    @ResponseBody
    @RequestMapping("/member/login")
    public Result login(HttpServletResponse response, @RequestBody Map map) {
        //验证码校验
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        if (validateCode != null && code != null && code.equals(validateCode)) {
            //是否是会员
            Member member = memberService.findByTelePhone(telephone);
            if (member == null) {
                member = new Member();
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());
                memberService.add(member);
            }
            //埋点
            Cookie cookie = new Cookie("login_telephone", telephone);
            cookie.setPath("/");
            cookie.setMaxAge(60 * 60 * 24);
            response.addCookie(cookie);

            String json = JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone, 60 * 30, json);
            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } else {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    @RequestMapping("/check")
    public String check(HttpServletRequest request, HttpServletResponse response) {
       //通过cookie是否存在telephone判断是否登录
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if (name != null && "login_telephone".equals(name)){
                    return "/pages_login/member.html";
                }
            }
        }
        return "/pages/login.html";
    }

    @ResponseBody
    @RequestMapping("/member/findmember")
    public Result findMember(HttpServletRequest request) {
        String telephone = null;
        Cookie[] cookies = request.getCookies();
        try {
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if (name != null && "login_telephone".equals(name)) {
                        telephone = cookie.getValue();
                    }
                }
            }
            Member member = memberService.findByTelePhone(telephone);
            return new Result(true,"查询会员成功",member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询会员失败");
        }
    }

    @ResponseBody
    @RequestMapping("/member/updateById")
    public Result updateById(@RequestBody Member member) {
        try {
            memberService.update(member);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }
}
